package org.onvivals.admin.module.pms.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.undertow.util.BadRequestException;
import org.onvivals.common.web.PageResult;
import org.onvivals.common.web.Result;
import org.onvivals.module.ums.entity.SysGroupData;
import org.onvivals.module.ums.param.SysGroupDataParam;
import org.onvivals.module.ums.service.ISysGroupDataService;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Mr.ZS
 * @Date: 2024/2/20 12:11
 * @company 湖南葆龄科技有限公司
 **/
@RestController
@RequestMapping("/yxSystemGroupData")
public class SystemGroupDataController {

	private final ISysGroupDataService yxSystemGroupDataService;

	public SystemGroupDataController(ISysGroupDataService yxSystemGroupDataService) {
		this.yxSystemGroupDataService = yxSystemGroupDataService;
	}

	@Operation(summary = "查询数据配置")
	@GetMapping()
	public Result<PageResult<SysGroupData>> page(SysGroupDataParam param) {
		return Result.OK(yxSystemGroupDataService.pageRel(param));
	}

	@Operation(summary =  "新增数据配置")
	@PostMapping()
	public Result<?> create(@RequestBody String jsonStr) throws BadRequestException {
		JSONObject jsonObject = JSON.parseObject(jsonStr);
		this.checkParam(jsonObject);
		SysGroupData yxSystemGroupData = new SysGroupData();
		yxSystemGroupData.setGroupName(jsonObject.get("groupName").toString());
		jsonObject.remove("groupName");
		yxSystemGroupData.setValue(jsonObject.toJSONString());
		yxSystemGroupData.setStatus(jsonObject.getInteger("status"));
		yxSystemGroupData.setSort(jsonObject.getInteger("sort"));
		yxSystemGroupDataService.save(yxSystemGroupData);
		jsonObject.put("id",yxSystemGroupData.getId());
		yxSystemGroupData.setValue(jsonObject.toJSONString());
		yxSystemGroupDataService.updateById(yxSystemGroupData);
		return Result.OK();
	}

	@Operation(summary =  "修改数据配置")
	@PutMapping()
	public Result<?> update(@RequestBody String jsonStr) throws BadRequestException {
		JSONObject jsonObject = JSON.parseObject(jsonStr);
		this.checkParam(jsonObject);

		SysGroupData yxSystemGroupData = new SysGroupData();

		yxSystemGroupData.setGroupName(jsonObject.get("groupName").toString());
		jsonObject.remove("groupName");
		yxSystemGroupData.setValue(jsonObject.toJSONString());
		yxSystemGroupData.setStatus(jsonObject.getInteger("status"));

		if (jsonObject.getInteger("status") == null) {
			yxSystemGroupData.setStatus(1);
		} else {
			yxSystemGroupData.setStatus(jsonObject.getInteger("status"));
		}

		if (jsonObject.getInteger("sort") == null) {
			yxSystemGroupData.setSort(0);
		} else {
			yxSystemGroupData.setSort(jsonObject.getInteger("sort"));
		}
		yxSystemGroupData.setId(Integer.valueOf(jsonObject.get("id").toString()));
		yxSystemGroupDataService.saveOrUpdate(yxSystemGroupData);
		return Result.OK("修改成功", yxSystemGroupData);
	}

	@Operation(summary =  "删除数据配置")
	@DeleteMapping(value = "/{id}")
	public Result<?> delete(@PathVariable Integer id) {
		yxSystemGroupDataService.removeById(id);
		return Result.OK();
	}

	/**
	 * 检测参数
	 *
	 * @param jsonObject
	 */
	private void checkParam(JSONObject jsonObject) throws BadRequestException {
		if (ObjectUtil.isNotNull(jsonObject.get("name"))) {
			if (StrUtil.isEmpty(jsonObject.getString("name"))) {
				throw new BadRequestException("名称必须填写");
			}
		}

		if (ObjectUtil.isNotNull(jsonObject.get("title"))) {
			if (StrUtil.isEmpty(jsonObject.getString("title"))) {
				throw new BadRequestException("标题必须填写");
			}
		}

		if (ObjectUtil.isNotNull(jsonObject.get("pic"))) {
			if (StrUtil.isEmpty(jsonObject.getString("pic"))) {
				throw new BadRequestException("图片必须上传");
			}
		}


		if (ObjectUtil.isNotNull(jsonObject.get("info"))) {
			if (StrUtil.isEmpty(jsonObject.getString("info"))) {
				throw new BadRequestException("简介必须填写");
			}
		}

	}
}
